package com.zhupf.part7;

import java.util.HashMap;
import java.util.List;

/**
 * @author zhupf
 * @date 2023年11月21日 10:15
 * @Description
 */
public class SortList {

    public static void main(String[] args) {
//        ListNode head = new ListNode(-1,new ListNode(5,new ListNode(3,new ListNode(4,new ListNode(0,new ListNode(6))))));
        ListNode head = new ListNode(4,new ListNode(2,new ListNode(1,new ListNode(3))));
        ListNode listNode = sortList2(head);
        System.out.println(listNode);
    }
    public static ListNode sortList(ListNode head){
        return sortList(head,null);
    }


    public static  ListNode sortList2(ListNode head) {
        if(head == null || head.next == null){
            return head;
        }
        ListNode midNode = findMidNode(head);
        ListNode midNodeNext = midNode.next;
        midNode.next = null;
        return mergSortListNode(sortList2(head),sortList2(midNodeNext));
    }

    public static  ListNode findMidNode(ListNode node){
        ListNode slow = node;
        ListNode fast = node.next;
        while (fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }

    public static  ListNode mergSortListNode(ListNode node1,ListNode node2){
        ListNode pre = new ListNode(0,null);
        ListNode res = pre;
        while (node1 != null && node2 != null){
            if(node1.val<= node2.val){
                pre.next = node1;
                node1 = node1.next;
            }else {
                pre.next = node2;
                node2 = node2.next;
            }
            pre = pre.next;
        }
        pre.next = node1 ==null?node2:node1;
        return res.next;
    }

    public static ListNode sortList(ListNode head,ListNode tail){
        if (head == null) {
            return head;
        }
        if (head.next == tail) {
            head.next = null;
            return head;
        }
        ListNode slow = head, fast = head;
        while (fast != tail) {
            slow = slow.next;
            fast = fast.next;
            if (fast != tail) {
                fast = fast.next;
            }
        }
        ListNode mid = slow;
        ListNode list1 = sortList(head, mid);
        ListNode list2 = sortList(mid, tail);
        ListNode sorted = merge(list1, list2);
        return sorted;
    }



    public static ListNode sortList1(ListNode head){
        if(head == null){
            return head;
        }
        ListNode dumy = new ListNode(0,head);
        int len = 0;
        while (head != null){
            len++;
            head = head.next;
        }
        for (int k = 1 ; k < len ; k <<=1){
            ListNode pre = dumy ,  cur = dumy.next;
            while (cur != null){
                ListNode head1 = cur;
                for (int i = 1 ; i < k && cur.next != null ; i ++){
                    cur = cur.next;
                }
                ListNode head2 = cur.next;
                cur.next = null;
                cur = head2;
                for (int i = 1 ; i < k && cur != null && cur.next != null ; i ++){
                    cur = cur.next;
                }
                ListNode next = null;
                if(cur != null){
                    next = cur.next;
                    cur.next = null;
                }
                ListNode merge = merge(head1,head2);
                pre.next = merge;
                while (pre.next != null){
                    pre = pre.next;
                }
                cur = next;
            }
        }
        return dumy.next;
    }


    private static ListNode merge(ListNode l1, ListNode l2) {
        if (l2 == null) {
            return l1;
        }
        ListNode pre = new ListNode(0);
        ListNode dumy = pre;
        while (l1 != null && l2 != null) {
            if (l1.val <= l2.val) {
                pre.next = l1;
                l1 = l1.next;
            } else {
                pre.next = l2;
                l2 = l2.next;
            }
            pre = pre.next;
        }
        pre.next = l1 == null ? l2 : l1;
        return dumy.next;
    }

    static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
}
